package com.jsoft.jsofted.platform.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsoft.jsofted.framework.mybatis.CocQueryWrapper;
import com.jsoft.jsofted.framework.service.impl.CocServiceImpl;
import com.jsoft.jsofted.framework.utils.ObjectUtils;
import com.jsoft.jsofted.platform.entity.ProjectInfoEntity;
import com.jsoft.jsofted.platform.mapper.ProjectInfoMapper;
import com.jsoft.jsofted.platform.service.ProjectInfoService;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Service
public class ProjectInfoServiceImpl extends CocServiceImpl<ProjectInfoMapper, ProjectInfoEntity> implements ProjectInfoService {

    /**
     * //////////////重写查询parseQueryWrapper///////////////////////////////////
     * 见：CocServiceImpl.java 280行处，自己定义查询的queryWrapper
     * @param queryParams
     * @return
     */
    protected CocQueryWrapper parseQueryWrapper(Map queryParams) {
        if (queryParams == null) {
            queryParams = new LinkedHashMap();
        }

        CocQueryWrapper queryWrapper = new CocQueryWrapper();


        // 调用原始框架的排序parseSortParams
        // 如果sortField排序字段存在，那么sortOrder=desc,或者asc进行排序
        this.parseSortParams(queryWrapper, queryParams);

        ///////////////////////////重写parseSimpleParams///////////////////////////////
        // 简单参数
        // 使用重写的parseSimpleParams
        this.parseSimpleParams(queryWrapper, queryParams);

        ///////////////////////////下面三个：新增，未重写///////////////////////////////
        // 关键字
        // 可查询字段（不够自己添加）：customerName、customerRegAddress、projName、projAddressName、serviceRange、prospect、otherRequire
        this._parseKeywordsParams(queryWrapper, queryParams);
        // 项目安全大类:securityCategory1
        // 项目安全细类:securityCategory2
        this._parseSecurityCategoryParams(queryWrapper, queryParams);
        // 时间区间
        this._parseCreatedDateParams(queryWrapper, queryParams);

        return queryWrapper;
    }
    /**
     * ////////////////////重写parseSimpleParams/////////////
     *   解析【查询参数】中的【简单参数】
     */
    protected void parseSimpleParams(CocQueryWrapper queryWrapper, Map queryParams) {
        Iterator<String> keys = queryParams.keySet().iterator();
        while (keys.hasNext()) {
            String key = keys.next();
            Object val = queryParams.get(key);
            // 排除分页排序参数
            if ("sortField".equals(key)
                    || "sortOrder".equals(key)
                    || "pageIndex".equals(key)
                    || "pageSize".equals(key)
            ) {
                continue;
            }
            //////////////////////重写,不要原始的关机键字查询/////////////
            // *********关键字查询（已经自定义：_parseKeywordsParams）***********
            //if ("keywords".equals(key)) {
            //queryWrapper.like("name", val);
            // continue;
            //}

            // 按字段名查询
            if (val instanceof List) {
                queryWrapper.regexp(StringUtils.camelToUnderline(key), ObjectUtils.join((List) val, "|"));
            } else {
                queryWrapper.eq(StringUtils.camelToUnderline(key), val);
            }

        }
    }
    ///////////////////////////////////////////////////////下面三个：新增，未重写//////////////////////////////////////////////////////////////
    /**
     * 解析【条件参数】中的【创建时间参数】
     * 如：
     *     0: "2021-07-09"
     *     1: "2021-07-18"
     */
    private void _parseCreatedDateParams(CocQueryWrapper queryWrapper, Map condParams) {
        List createTimes = (List) condParams.get("setupTimes");
        if (createTimes != null && createTimes.size() == 2) {
            queryWrapper.ge("setup_time", createTimes.get(0) + " 00:00:00");
            queryWrapper.le("setup_time", createTimes.get(1) + " 23:59:59");
        }
    }

    /**
     * 解析【条件参数】中的【关键字参数】
     * 可查询字段（不够自己添加）：customerName、customerRegAddress、projName、projAddressName、serviceRange、prospect、otherRequire
     */
    private void _parseKeywordsParams(CocQueryWrapper queryWrapper, Map condParams) {
        String keywords = (String) condParams.get("keywords");
        if (keywords == null || keywords.trim().length() == 0) {
            return;
        }

        Consumer<CocQueryWrapper> consumer = qw -> {
            qw.like("customer_name", keywords);
            qw.or();
            qw.like("customer_reg_address", keywords);
            qw.or();
            qw.like("proj_name", keywords);
            qw.or();
            qw.like("proj_address_name", keywords);
            qw.or();
            qw.like("service_range", keywords);
            qw.or();
            qw.like("prospect", keywords);
            qw.or();
            qw.like("other_require", keywords);
        };
        queryWrapper.and(consumer);
    }

    /**
     * 解析【条件参数】中的【项目安全大类、项目安全细类参数】
     */
    private void _parseSecurityCategoryParams(CocQueryWrapper queryWrapper, Map condParams) {
        String securityCategory1 = (String) condParams.get("securityCategory1");
        String securityCategory2 = (String) condParams.get("securityCategory2");
        if (securityCategory1 != null && securityCategory1.trim().length() > 0) {
            queryWrapper.eq("security_category1", securityCategory1);
        }
        if (securityCategory2 != null && securityCategory2.trim().length() > 0) {
            queryWrapper.eq("security_category2", securityCategory2);
        }
    }

    /**
     * mybatis查询-分页
     * @param projectInfoEntity
     * @return 查询结果
     */

    @Override
    public Page<ProjectInfoEntity>  selectProjectInfoEntityList(Page<ProjectInfoEntity> page, ProjectInfoEntity projectInfoEntity) {
        List<ProjectInfoEntity> list = baseMapper.selectProjectInfoEntityList(page,projectInfoEntity);
        return page.setRecords(list);
    }
    /**
     * mybatis插入
     * @param projectInfoEntity
     * @return 插入数量
     */
    @Override
    public int insertProjectInfoEntity(ProjectInfoEntity projectInfoEntity) {
        return baseMapper.insertProjectInfoEntity(projectInfoEntity);
    }
    /**
     *mybatis更新(依据id)
     * @param projectInfoEntity
     * @return 更新数量
     */
    @Override
    public int updateProjectInfoEntity(ProjectInfoEntity projectInfoEntity) {
        return baseMapper.updateProjectInfoEntity(projectInfoEntity);
    }
    /**
     * 删除
     * @param id
     * @return 删除数量
     */
    @Override
    public int deleteProjectInfoEntityById(Long id) {
        return baseMapper.deleteProjectInfoEntityById(id);
    }
    /**
     * 删除-批量
     * @param ids
     * @return 删除数量
     */
    @Override
    public int deleteProjectInfoEntityByIds(List<Long> ids) {
        return baseMapper.deleteProjectInfoEntityByIds(ids);
    }
}
